/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mam.rbac.application.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.AccountStatus;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.common.utils.web.QuerySpecs;
import com.foreveross.bsl.mam.model.rbac.domain.Admin;
import com.foreveross.bsl.mam.rbac.application.AdminApplication;
import com.foreveross.bsl.mam.rbac.application.vo.AdminVO;

/**
 * @description
 * 
 * @author rening
 * @version v1.0
 * 
 * @2013-6-7
 * 
 */
@Named("adminApplication")
public class AdminApplicationImpl implements AdminApplication {
 
	@Override
	public AdminVO saveEntity(AdminVO entity) {
		Admin admin = new Admin();
		BeanHelper.copyProperties(admin, entity);
		if(admin.getStatus() == null){
			admin.setStatus(AccountStatus.Normal.getValue());
		}
		Md5PasswordEncoder md = new Md5PasswordEncoder();
		String encodePassword = md.encodePassword(entity.getPassword(),
				entity.getUsername());

		admin.setPassword(encodePassword);
		admin.setCreateDate(new Date());
		admin.save();
		BeanHelper.copyProperties(entity, admin);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public AdminVO updateEntity(String id, AdminVO entity) {
		Admin admin = new Admin();
		admin = (Admin)admin.findOne(id);
		BeanHelper.copyProperties(admin, entity);
		admin.save();
		BeanHelper.copyProperties(entity, admin);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(String id) {
		Admin admin = new Admin();
		admin = (Admin)admin.findOne(id);
		admin.delete();
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public AdminVO getEntity(String id) {
		Admin admin = new Admin();
		admin = (Admin)admin.findOne(id);
		AdminVO vo = new AdminVO();
		BeanHelper.copyProperties(vo, admin);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<AdminVO> findAll() {
		Admin admin = new Admin();
		Iterable iterable = admin.findAll();
		if(iterable != null){
			List<AdminVO> result = new ArrayList<AdminVO>();
			AdminVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				admin = (Admin) iterator.next();

				vo = new AdminVO();
				BeanHelper.copyProperties(vo, admin);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.AdminApplication#findByPageable(org.springframework.data.domain.Pageable, com.foreveross.bsl.system.application.vo.AdminVO)
	 */
	@Override
	public Page<AdminVO> findByPageable(int pageNumber, int pageSize, QuerySpecs querySpecs) {
		Admin acc = new Admin();
		//AdminRepository r = acc.getEntityRepository(AdminRepository.class);
		Pageable pageable = new PageRequest(pageNumber, pageSize);
		Page page = acc.findByQuerySpecs(querySpecs, pageable);
		if(page != null && page.getContent() != null){
			List<AdminVO> result = new ArrayList<AdminVO>();
			AdminVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				acc = (Admin) iterator.next();

				vo = new AdminVO();
				BeanHelper.copyProperties(vo, acc);
				result.add(vo);
			}
			Page<AdminVO> pageVO = new PageImpl<AdminVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public AdminVO getEntityByUserName(String username) {
		Admin admin = new Admin();
		OperatEnum operat = OperatEnum.EQ;
		String proName = "username";
		List<Admin> admins = (List<Admin>)admin.findListByProperty(proName, operat,username);
		if(!admins.isEmpty()){
			AdminVO vo = new AdminVO();
			BeanHelper.copyProperties(vo, admins.get(0));
			return vo;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public AdminVO getEntityByUserNameAndPassword(String username,String password) {
		Admin admin = new Admin();
		OperatEnum[] operats = new OperatEnum[]{OperatEnum.EQ,OperatEnum.EQ};
		String[] proName = new String[]{"username","password"};
		
		Md5PasswordEncoder md = new Md5PasswordEncoder();
		String encodePassword = md.encodePassword(password, username);
		String[] values = new String[]{username,encodePassword};
		
		List<Admin> admins = (List<Admin>)admin.findListByProperty(proName, operats,values);
		if(admins !=null && admins.size()>0){
			AdminVO vo = new AdminVO();
			BeanHelper.copyProperties(vo, admins.get(0));
			return vo;
		}
		
		return null;
	}
}
